package com.zdft.promotion.promotion.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdft.promotion.common.service.BusinessException;
import com.zdft.promotion.common.util.RestfulResult;
import com.zdft.promotion.promotion.bean.PromotionRulesBean;
import com.zdft.promotion.promotion.model.PromotionRulesFilterModel;
import com.zdft.promotion.promotion.service.api.IPromotionRangeService;
import com.zdft.promotion.promotion.service.api.IPromotionRulesService;
import com.zdft.promotion.promotion.vo.PromotionRulesResponseVO;
import com.zdft.promotion.promotion.vo.PromotionRulesVO;
import com.zdft.promotion.sso.util.AuthenticationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Date;


/**
 * 促销流程
 *
 * @author wangtao
 * @email 
 * @date 2020-02-24 11:03:08
 */
@Api(description = "促销流程管理控制器")
@RestController
@RequestMapping("promotion/rules")
public class PromotionRulesController {
    @Autowired
    private IPromotionRulesService promotionRulesService;
    @Autowired
    private IPromotionRangeService promotionRangeService;

    @ApiOperation("通过主键查询")
    @GetMapping("/findById")
    public PromotionRulesBean findById(@RequestParam Long id){
        return promotionRulesService.getById(id);
    }

    @ApiOperation("促销流程查询")
    @GetMapping("/findAll")
    public PromotionRulesResponseVO findAll(@RequestBody PromotionRulesFilterModel model){
        QueryWrapper<PromotionRulesBean> query = new QueryWrapper();
        if(model.getId() != null){
            query.lambda().eq(PromotionRulesBean::getId, model.getId());
        }
        if(model.getStatus() != null){
            query.lambda().eq(PromotionRulesBean::getStatus, model.getStatus());
        }
        if(StringUtils.isNotBlank(model.getPromotionName())){
            query.lambda().eq(PromotionRulesBean::getPromotionName, model.getPromotionName());
        }
        if(StringUtils.isNotBlank(model.getStartDate())){
            query.lambda().ge(PromotionRulesBean::getStartDate, model.getStartDate());
        }
        if(StringUtils.isNotBlank(model.getEndDate())){
            query.lambda().le(PromotionRulesBean::getEndDate, model.getEndDate());
        }
        Page<PromotionRulesBean> page = new Page(model.getCurrent(), model.getSize());
        IPage<PromotionRulesBean> result = promotionRulesService.page(page, query);
        return new PromotionRulesResponseVO(result.getTotal(), result.getCurrent(), result.getSize(), result.getRecords());
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    public RestfulResult save(@RequestBody @Valid PromotionRulesVO vo){
        PromotionRulesBean bean = new PromotionRulesBean();
        try {
            BeanUtils.copyProperties(bean, vo);
            bean.setCreateTime(new Date());
            bean.setCreateUser(AuthenticationUtil.get().getUserBase().getEmpCode());
            promotionRulesService.save(bean);
        } catch (BeansException e) {
            e.printStackTrace();
        }
        return new RestfulResult();
    }

    @ApiOperation("修改")
    @PostMapping("/modify")
    public RestfulResult modify(@RequestBody @Valid PromotionRulesVO vo){
        PromotionRulesBean bean = new PromotionRulesBean();
        BeanUtils.copyProperties(bean, vo);
        bean.setUpdateTime(new Date());
        bean.setUpdateUser(AuthenticationUtil.get().getUserBase().getEmpCode());
        promotionRulesService.updateById(bean);
        return new RestfulResult();
    }

    @ApiOperation("删除")
    @GetMapping("/delete")
    public RestfulResult delete(@RequestParam @NotNull(message = "要删除的主键不能为空") Long id){
        promotionRulesService.delete(id);
        return new RestfulResult();
    }

    @ApiOperation("提交前校验")
    @GetMapping("/check")
    public RestfulResult check(@RequestParam Long id){
        PromotionRulesBean bean = promotionRulesService.getById(id);
        if(bean.getStatus().intValue() != 1){
            throw new BusinessException("-1", "只能提交状态为新建的数据");
        }
        int count = promotionRangeService.findByPromotionRulesId(id);
        if(count == 0){
            throw new BusinessException("-1", "请先维护促销范围");
        }
        return new RestfulResult();
    }

    @ApiOperation("提交")
    @GetMapping("/submit")
    public RestfulResult submit(@RequestParam Long id){

        return new RestfulResult();
    }

}
